package com.example.baidumap_a.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import com.example.baidumap_a.R;
import com.example.baidumap_a.entity.Ohuang;

public class ChangePawActivity extends AppCompatActivity {
    private EditText editText_oldpsw, editText_psw, editText_psw2;
    private String name;
    private String oldpsw, password1, password2;
    private Dialog dialogLoading;
    private static final int RE_SUCCESS = 0, RE_FAIL = 1, RE_ERROR = 2, RE_OTHER = 3;
    final OkHttpClient client = new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        name = intent.getStringExtra("username");
        setContentView(R.layout.activity_change_paw);
        editText_oldpsw = findViewById(R.id.edit_oldPaw_ChangePaw);
        editText_psw = findViewById(R.id.edit_password_ChangePaw);
        editText_psw2 = findViewById(R.id.edit_password2_ChangePaw);
    }

    public void CloseChangePawClick(View view) {
        finish();
    }


    //发起更改密码请求
    private void PostHttpRequest() {
        RequestBody formBody = new FormBody.Builder()
                .add("username", name)
                .add("password", oldpsw)
                .add("password3",password1)
                .build();

        final Request request = new Request.Builder()
                .url("https://d30n971042.imdo.co/user/change")
                .post(formBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myHandler.sendEmptyMessage(RE_ERROR);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ResponseBody body = response.body();
                if (body != null) {
                    String code = body.string();
                    if ("success".equals(code)) {
                        myHandler.sendEmptyMessage(RE_SUCCESS);
                    } else if ("error".equals(code)) {
                        myHandler.sendEmptyMessage(RE_FAIL);
                    } else {
                        myHandler.sendEmptyMessage(RE_OTHER);
                    }
                    body.close();
                }
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case RE_SUCCESS:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改成功", "密码更改成功", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                    break;
                case RE_FAIL:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改失败", "密码更改失败,请检查旧密码是否正确");
                    break;
                case RE_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改失败", "网络连接错误");
                    break;
                case RE_OTHER:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改失败", "返回值错误");
                    break;

            }
        }
    };

    //更改密码按钮
    public void ChangePawClick(View view) {
        oldpsw = editText_oldpsw.getText().toString();
        password1 = editText_psw.getText().toString();
        password2 = editText_psw2.getText().toString();
        if (oldpsw.isEmpty() || password1.isEmpty() || password2.isEmpty()) {
            Ohuang.dialogshow(this, "更改失败", "密码不能为空。");
            return;
        }
        if (!password1.equals(password2)) {
            Ohuang.dialogshow(this, "更改失败", "两次密码输入不一致。");
            return;
        }
        dialogLoading = Ohuang.dialogLoadingshow(ChangePawActivity.this, "更改中");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    PostHttpRequest();//发起更改密码请求
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
}
